package com.kerpow.KSheet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;


public class GlobalSettings {
	
	public static boolean snappyPowers;
	public static int menuActivation;
	
	public static void Save(Context c){
		SharedPreferences p = c.getSharedPreferences(c.getString(R.string.app_name), Context.MODE_PRIVATE);
		Editor e = p.edit();
		
		e.putBoolean("snappyPowers", snappyPowers);
		e.putInt("menuActivation", menuActivation);
		e.commit();
		Log.i("kerpow","Settings saved");
	}
	
	public static void Load(Context c){
		SharedPreferences p = c.getSharedPreferences(c.getString(R.string.app_name), Context.MODE_PRIVATE);
		
		GlobalSettings.snappyPowers = p.getBoolean("snappyPowers", true);
		GlobalSettings.menuActivation = p.getInt("menuActivation", 0);
		Log.i("kerpow","Settings loaded");
	}

	public static String md5(String path) {  
	    try {  
	    	//get the bytes
	    	File file = new File(path);
	    	
	    	InputStream is = new FileInputStream(file);
	    	
	        // Get the size of the file
	        long length = file.length();

	        // You cannot create an array using a long type.
	        // It needs to be an int type.
	        // Before converting to an int type, check
	        // to ensure that file is not larger than Integer.MAX_VALUE.
	        if (length > Integer.MAX_VALUE) {
	            // File is too large
	        }

	        // Create the byte array to hold the data
	        byte[] bytes = new byte[(int)length];

	        // Read in the bytes
	        int offset = 0;
	        int numRead = 0;
	        while (offset < bytes.length
	               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
	            offset += numRead;
	        }

	        // Close the input stream and return bytes
	        is.close();
	    	
	    	
	        // Create MD5 Hash  
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");  
	        digest.update(bytes);  
	        byte messageDigest[] = digest.digest();  
	          
	        // Create Hex String  
	        StringBuffer hexString = new StringBuffer();  
	        for (int i=0; i<messageDigest.length; i++)  
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        
	        return hexString.toString();  
	          
	    } catch (NoSuchAlgorithmException e) {  
	        e.printStackTrace();  
	    } catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	    return "";  
	}  
}
